function changeStatus(formname, id, status, page, sortfield, ascdesc){   
    $('form[name="'+formname+'"]').append('<input type="hidden" name="id" id="id" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="func" id="func" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="status" id="status" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="pagenum" id="pagenum" />');   
    $('form[name="'+formname+'"]').append('<input type="hidden" name="sortfield" id="sortfield" />');   
    $('form[name="'+formname+'"]').append('<input type="hidden" name="ascdesc" id="ascdesc" />');   
    
    $('#id').val(id);
    $('#func').val('status');
    $('#status').val(status);
    if($('#pagenum')!=null) $('#pagenum').val(page);  
    if($('#sortfield')!=null) $('#sortfield').val(sortfield);  
    if($('#ascdesc')!=null) $('#ascdesc').val(ascdesc);  

    document.forms[formname].submit();
}
function sortfield(formname, sortfield, ascdesc){
    $('form[name="'+formname+'"]').append('<input type="hidden" name="sortfield" id="sortfield" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="ascdesc" id="ascdesc" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="pagenum" id="pagenum" />');  
   
    if($('#sortfield')) $('#sortfield').val(sortfield);
    if($('#ascdesc')) $('#ascdesc').val(ascdesc);
    if($('#pagenum')) $('#pagenum').val(0); 
    document.forms[formname].method = "get";
    document.forms[formname].action += window.location.search;
    document.forms[formname].submit();
}

function delRecord(formname, id, page, msg, sortfield, ascdesc){
    $('form[name="'+formname+'"]').append('<input type="hidden" name="id" id="id" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="func" id="func" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="pagenum" id="pagenum" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="sortfield" id="sortfield" />');   
    $('form[name="'+formname+'"]').append('<input type="hidden" name="ascdesc" id="ascdesc" />');   
   
    if (confirm(msg)){
        if($('#id')!=null) $('#id').val(id);
        $('#func').val('del');
        if($('#pagenum')!=null) $('#pagenum').val(page);   
        if($('#sortfield')!=null) $('#sortfield').val(sortfield);  
        if($('#ascdesc')!=null) $('#ascdesc').val(ascdesc);  
        document.forms[formname].submit();
    }
}

function delitem(formname, itemid, fileid, status, msg){
    $('form[name="'+formname+'"]').append('<input type="hidden" name="fileid" id="fileid" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="func" id="func" />');
    $('form[name="'+formname+'"]').append('<input type="hidden" name="id" id="id" />');
    
    if (confirm(msg)){
        $("#id").val(itemid);
        $('#fileid').val(fileid);
        $('#func').val(status);     
        document.forms[formname].method="post";
        document.forms[formname].submit();
    }
}

function edit(action, id, page){
    var link=action+"?id="+id;
    if (page>0)link+="&pagenum="+page;
    window.location.href=link;
}

function selectAjax(url, find_id, type, sel_name, sel_value, lang, hasDefault){
    $.getJSON(url, {
        obj:find_id, 
        type:type
    }, function(json){
        var html='<option value="">'+lang+'</option>';
        if(json){
            if(!hasDefault)
                html='';
            $.each(json, function(k, v){
                html+='<option value="'+k+'">'+v+'</option>';
            });
        } 
        $('select[name="'+sel_name+'"]').html(html).val(sel_value);
    });
}

function checkObjectAjax(url, check_obj, type, show_obj, str_success, str_failure, return_value){
    if($('#'+show_obj+'_boolean').length==0)
        $('.TableA').append('<input type="hidden" name="'+show_obj+'_boolean" id="'+show_obj+'_boolean" />');
    $.getJSON(url, {
        obj:check_obj, 
        type:type,
        return_value:return_value
    }, function(json){
        if(json){
            setMsg(show_obj, str_success, 'green');
            $('#'+show_obj+'_boolean').val("true");
        }
        else{
            setMsg(show_obj, str_failure, 'red');        
            $('#'+show_obj+'_boolean').val("false");
        }
    });
}
function checkspecAjax(url, spec_no, n){
    $.getJSON(url, {
        spec_no:spec_no
    }, function(json){
        var msg_str=json[0].msg;
        alert(''+msg_str);
        n=n-1;
        $('input[name="list_spec[]"]').eq(n).val(json[0].spec);
        $('input[name="list_weight[]"]').eq(n).val(json[0].weight);
        $('input[name="list_storage[]"]').eq(n).val(json[0].storage);
          
    });
}

function windowopen(url){
    window.open(url, '', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=n o, status=no')
}


function changeLang(lg){
    var f = document.createElement("form");
    f.method="post";
    //    f.method="post";
 
    var input = document.createElement("input");
    input.type="hidden";
    input.name="lg";
    input.value=lg;
    f.appendChild(input);
    $("body").append(f);
    f.submit();
}
function QueryString(name)
{
    var AllVars = window.location.search.substring(1);
    var Vars = AllVars.split("&");
    for (i = 0; i < Vars.length; i++)
    {
        var Var = Vars[i].split("=");
        if (Var[0] == name) return Var[1];
    }
    return "";
}

function setMsg(obj, msg, color){     
    $('#'+obj).text(msg);
    $('#'+obj).attr("style", "color:"+color);
}

function setRequired(obj, msg){ 
    removeObjErrorMsg(obj);
    $('#'+obj).addClass('bgred');
    $('#'+obj).parent().find("label.error").remove();
    $('#'+obj).parent().append('<label class="error">'+msg+'</label>');
    
}
function removeObjErrorMsg(obj){
    $('#'+obj).click(function(){
        $(this).removeClass('bgred');
        $(this).parent().find("label").remove();
    });
    $('#'+obj).keydown(function(){
        $(this).removeClass('bgred');
        $(this).parent().find("label").remove();
    });         
}
function trim(value){
    if (value)
        return value.replace(/(^[\\s]*)|([\\s]*$)/g, "");
    else return '';
}
function checkCode(val){ 
    var reg = /^\w*\s*\w*$/; 
    return reg.test(val); 
}


function addBookmarkForBrowser(sTitle, sUrl) {
    if (window.sidebar && window.sidebar.addPanel) {
        window.sidebar.addPanel(sTitle, sUrl, "");
    } else if (window.external) {
        window.external.AddFavorite(sUrl, sTitle);
    } else {
        alert("您的瀏覽器不支援此功能!");
    }
}


function share(){
    var obj=this;
    obj.linksrc='';
    obj.url='';
    obj.title='';
    obj.sharelink=function (type){
        if(type=='facebook'){
            var linksrc1="http://www.facebook.com/share.php?";
            var linksrc2="u="+encodeURIComponent(obj.url);
            var linksrc3="&t="+encodeURIComponent(obj.title);
            obj.linksrc=linksrc1+linksrc2+linksrc3;
        }else if(type=='plurk'){
            var linksrc1="http://www.plurk.com/?qualifier=shares";
            var linksrc2="&status="+encodeURIComponent(obj.url);
            var linksrc3="("+encodeURIComponent(obj.title)+")";
            obj.linksrc=linksrc1+linksrc2+"+"+linksrc3;
        }else if(type=='twitter'){
            var linksrc1="http://twitter.com/home?";
            var linksrc2="status="+encodeURIComponent(obj.title+obj.url);
            var linksrc3="";
            obj.linksrc=linksrc1+linksrc2+linksrc3;
        }else if(type=='google'){
            var linksrc1="http://plus.google.com/share?";
            var linksrc2="url="+encodeURIComponent(obj.title+obj.url);
            var linksrc3="&hl=zh-TW";
            obj.linksrc=linksrc1+linksrc2+linksrc3;
        }
        
        if(obj.linksrc.length>0) void(window.open(obj.linksrc));
        return true;
    }
	
    obj.shareitem=function(type, url, title){
        obj.title=title;
        obj.url=url;    	   
        this.sharelink(type);
    }
}

function showAdd(item){
    $(".madd").hide();
    $(".madd").eq(item-1).show();
}

function previewScreen(block){
    var value = block.innerHTML;
    var printPage = window.open("","printPage","");
    printPage.document.open();
    printPage.document.write("<HTML><head></head><BODY onload='window.print();window.close()'>");
    printPage.document.write("<PRE>");
    printPage.document.write(value);
    printPage.document.write("</PRE>");
    printPage.document.close("</BODY></HTML>");
}